package com.sanbi.erp.core.util;

import com.github.houbb.property.bs.PropertyBs;
import com.sanbi.erp.core.config.FreightConfig;
import com.sanbi.erp.core.dto.GoodsDto;

import java.math.BigDecimal;

/**
 * @Desc:邮费业务工具类
 * @Data: 2022/8/24 13:15
 * @Author: WuHang
 */
public class FreightUtil {

    static FreightConfig freightConfig;

    static {
        freightConfig = new FreightConfig();
        PropertyBs.getInstance("common-config.properties").asBean(freightConfig);
    }

    /**
     * 单件商品详情邮费计算方法
     *
     * @return 邮费金额
     */
    public static BigDecimal getFreightPrices(GoodsDto goodsDto) {
        BigDecimal freightDecimal;
        float weight = goodsDto.getWeight().floatValue();//重量
        float volume = goodsDto.getCubageWeight().floatValue();//体积重
        if (volume >= weight){//体积重大于重量
            freightDecimal = getWeightPrice(volume);
        }else {//体积重小于重量
            freightDecimal = getWeightPrice(weight);
        }
        return freightDecimal;
    }



    /**
     * 重量体积重的计算方法
     * @param kg 重量
     * @return 满足区间的价格
     */
    private static BigDecimal getWeightPrice(float kg) {
        BigDecimal freightDecimal;
        if (kg > 0 && kg <= freightConfig.getFreightScale1()){// TODO   0 ~ 1
            freightDecimal = freightConfig.getFreightScale1Price();
        }
        else if (kg > freightConfig.getFreightScale1() && kg <= freightConfig.getFreightScale2()){// TODO   1 ~ 2
            freightDecimal = freightConfig.getFreightScale2Price();
        }
        else if (kg > freightConfig.getFreightScale2() && kg <= freightConfig.getFreightScale3()){// TODO   2 ~ 5
            freightDecimal = freightConfig.getFreightScale3Price();
        }
        else if (kg > freightConfig.getFreightScale3() && kg <= freightConfig.getFreightScale4()){// TODO   5 ~ 10
            freightDecimal = freightConfig.getFreightScale4Price();
        }
        else if (kg > freightConfig.getFreightScale4()){// TODO   10 ~ 正无穷
            freightDecimal = freightConfig.getFreightScale5Price();
        }else {
            freightDecimal = new BigDecimal("0.00");
        }
        return freightDecimal;
    }

    /**
     * 体积重计算方法
     *
     * @return 体积重
     */
    public static BigDecimal getFreightByCubageWeight(GoodsDto goodsDto) {
        //体积重 = 长 x 宽 x 高 / 体积参数 四舍五入保留一位小数
        return new BigDecimal(String.format("%.1f", (float)goodsDto.getLength() * goodsDto.getWidth() * goodsDto.getHeight() / freightConfig.getGoodsCubage()));
    }


}
